package com.blog.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blog.bean.User;
import com.blog.dao.IUserDao;

/**
 * @className: UserController
 * @description: 
 * @author: HCl
 * @createTime: 2020年6月7日 上午8:47:52
 */
@Controller
public class UserController {
	@Autowired
	private IUserDao userDao;
	
	@RequestMapping(value = "doRegister",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> doRegister(String username,String password,String email){
		Map<String, Object> reMap =  new HashMap<String,Object>();
		System.out.println(username+password+email);
		User user = new User();
		user.setUsername(username);
		
		user.setPassword(password);
		user.setName(username);
		user.setEmail(email);
		
		//
		User user2 = null;
		user2 = userDao.selectUserByUsername(user);
		
		if (user2 != null) {
			reMap.put("state", 0);
			reMap.put("msg","用户("+user2.getUsername()+")已存在！");
		} else {
			int num = userDao.addUser(user);
			if (num == 0) {
				reMap.put("state", 0);
				reMap.put("msg","系统忙碌，注册失败！");
			} else {

				reMap.put("state", 1);
				reMap.put("msg","用户("+user.getName()+")注册成功");
			}

		}
		
		
		return reMap;
	}

	
	@RequestMapping(value = "findUser",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> findUser(String username){
		Map<String, Object> reMap = new HashMap<String, Object>();
		
		User user = new User();
		user.setUsername(username);

		User user2 = null;
		user2 = userDao.selectUserByUsername(user);
		
		if (user2 == null) {
			reMap.put("state", 1);
			reMap.put("msg", "用户名("+user.getUsername()+")可用！");
		} else {
			reMap.put("state", 0);
			reMap.put("msg", "用户名("+user.getUsername()+")已存在！");
		}
	
		return reMap;
		
	}
}
